﻿using System;
using System.Collections.Generic;
using Silent.MDB.Common.Services;

namespace Silent.MDB.Server.Model.Repositories
{
    public class RepositoryManagerDefault : IRepositoryManager
    {
        private readonly Dictionary<Type, object> m_repositories = new Dictionary<Type, object>();

        public RepositoryManagerDefault(string name)
        {
            Name = name;
        }

        public string Name { get; private set; }

        public void AddRepository<T>(IRepository<T> repository)
        {
            var type = typeof(T);
            if (m_repositories.ContainsKey(type))
                throw new ArgumentException("Repository of type IRepository<{0}> was already added", type.Name);

            m_repositories.Add(typeof(T), repository);
        }

        public IRepository<T> GetRepository<T>()
        {
            var type = typeof (T);
            if(!m_repositories.ContainsKey(type))
                throw new ArgumentException("Repository of type IRepository<{0}> was not added", type.Name);

            return m_repositories[typeof (T)] as IRepository<T>;
        }
    }
}
